package cn.itcast.order.service;

import cn.itcast.feign.client.ItemClient;
import cn.itcast.hmall.pojo.order.Order;
import cn.itcast.hmall.pojo.order.OrderDetail;
import cn.itcast.order.mapper.OrderDetailMapper;
import cn.itcast.order.mapper.OrderMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class XxjobService {
    //完成xxjob的定时任务设计
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @XxlJob("testJob")
    public void Test(){
        Date d=new Date() ;
        System.out.println("测试任务执行 "+d);
    }

    @XxlJob("deleOld")
    public void trim(){
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        Date date=new Date();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        String format = sdf.format(date);
        List<Order>list=orderMapper.deleteOld(format);
        List<OrderDetail> list1 = orderDetailMapper.deleteOld(format);
        System.out.println(list);

        for(OrderDetail o:list1){
            Long itemId = o.getItemId();
            Integer num = o.getNum();
            itemClient.comback(itemId.toString(),num);
        }
        for(Order o:list){
            o.setStatus(5);
            orderMapper.deleteById(o.getId());
            orderMapper.insert(o);
        }
    }
}
